package com.example.jetour_line.map.mapview.MarkerMap;

import android.content.Context;

import com.example.jetour_line.JetourLinePlugin;
import com.example.jetour_line.map.mapview.BaseLatLonPoint;
import com.example.jetour_line.map.mapview.BaseMapView;
import com.example.jetour_line.map.mapview.BaseMarkerModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MarkerMapView extends BaseMapView {
    int mapType = 0;//1 路书详情 2 共创详情  3 创建行程（共创）
    Context mContext;
    public MarkerMapView(Context context, Object obj) {
        super(context);
        mContext = context;
        Class<?> objClass = obj.getClass();
        Class<?> listClass =  ArrayList.class;
        if (objClass == listClass){
            ArrayList msg = (ArrayList) obj;
            if ( msg.size()>0){
                Map map = (Map) msg.get(0);
                String type = (String) map.get("type");
                if(type.equals("lineDetail")){//路书详情
                    mapType = 1;
                }else if(type.equals("commonDetail")){//共创详情
                    mapType = 2;
                }else if(type.equals("trajectoryDetail")){//共创详情
                    mapType = 3;
                } else{
                    mapType = 4;
                }
            }
        }
        if(mapType == 1 ||mapType == 2){
            addMarkers(obj);
        }else if (mapType == 3){

            addLineOverlay(obj);
        }
        setDriveColor("#00A198");
        JetourLinePlugin.mapMsgListener = new JetourLinePlugin.MapMsgListener() {
            @Override
            public void msgListener(String method, Object obj) {
                if(method.equals("addMarkers")){
                    mapType = 3;
                    addMarkers(obj);
                }
            }
        };
    }
    private  void addLineOverlay(Object obj){
        ArrayList nodes = (ArrayList) obj;
        List<BaseMarkerModel> gaoMarkerModels = new ArrayList<>();
        for (int i = 0;i<nodes.size();i++){
            Map map = (Map) nodes.get(i);
            BaseMarkerModel model = new BaseMarkerModel();
            model.setPoint(new BaseLatLonPoint((Double) map.get("lat"),(Double)map.get("lng")));

            gaoMarkerModels.add(model);
        }
        clearMap();
        addMarkerOptions(gaoMarkerModels);
        routePlanning(gaoMarkerModels);
        changeMapBound(gaoMarkerModels,100,100,100,100);
    }


    private  void addMarkers(Object obj){
        ArrayList nodes = (ArrayList) obj;
        List<BaseMarkerModel> gaoMarkerModels = new ArrayList<>();
        for (int i = 0;i<nodes.size();i++){
            Map map = (Map) nodes.get(i);
            BaseMarkerModel model = new BaseMarkerModel();
            model.setId(map.get("nodeId")+"");
            model.setPoint(new BaseLatLonPoint((Double) map.get("lat"),(Double)map.get("lng")));
            model.setObject(map);
            if(mapType == 1){
                LineDetailMarkerView markerView = new LineDetailMarkerView(mContext, (Integer) map.get("index"));
                model.setMarkerView(markerView);
            }else{
                CommonDetailMarkerView markerView = new CommonDetailMarkerView(mContext, (Integer) map.get("index"),null);
                model.setMarkerView(markerView);
            }
            gaoMarkerModels.add(model);
        }
        clearMap();
        addMarkerOptions(gaoMarkerModels);
        routePlanning(gaoMarkerModels);
        changeMapBound(gaoMarkerModels,100,100,100,100);
    }
}
